/**
 * 禁止输入空格
 */
function preventSpace() {
	let event = window.event;
	
	if(event.keyCode === 32) {
		event.returnValue = false;
	}
}

/**
 * 登录
 */
function login() {
	let username = $("#login_username").val();
    let password = $("#login_password").val();
    let code = $("#code").val();

	if (!username) {
		alert("请输入用户名！");
		
		$("#login_username").focus();
	} else if (!password) {
		alert("请输入密码！");
		
		$("#login_password").focus();
	} else {
		ajaxPost("/user/login", {
			username: username,
			password: password
		}, function() {
			location.href = "/index.html";
		}, function (res) {
			if (res && res.responseJSON) {
				let response = res.responseJSON;

				if (res.status && res.status === 404) {
					let message;

					if(response.path) {
						message = "路径" + response.path + "不存在。";
					} else {
						message = response.message;
					}

					alert(message);
				} else {
					alert(response.message);
				}
			} else {
				alert("请求无响应~");
			}
		});
	}
}

$(function() {
	$("#login_username").keydown(function() {
		preventSpace();
	}).attr("placeholder", "请输入用户名");

	/**
	 * 给密码输入框绑定回车登录事件
	 */
	$("#login_password").keydown(function() {
		preventSpace();

		let event = window.event;

		if(event.keyCode === 32) {
			event.returnValue = false;
		} else if(event.keyCode === 13) {
			login();
		}
	}).attr("placeholder", "请输入密码");

	/******************************************************************************************************/

	// 点击登录按钮
	$("#btn_Login").click(function () {
		login();
	});

	$(".content .con_right .left").on("click", function () {
		$(this).css({
			"color": "#333333",
			"border-bottom": "2px solid #2e558e"
		});
		$(".content .con_right .right").css({
			"color": "#999999",
			"border-bottom": "2px solid #dedede"
		});
		$(".content .con_right ul .con_r_left").css("display", "block");
		$(".content .con_right ul .con_r_right").css("display", "none");
	});

	$(".content .con_right .right").on("click", function () {
		$(this).css({
			"color": "#333333",
			"border-bottom": "2px solid #2e558e"
		});
		$(".content .con_right .left").css({
			"color": "#999999",
			"border-bottom": "2px solid #dedede"
		});
		$(".content .con_right ul .con_r_right").css("display", "block");
		$(".content .con_right ul .con_r_left").css("display", "none");
	});

});